using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCWeb.Models;
using System.IO;
using MVCWeb.Models.ViewData;
using System.Configuration;
using MVCWeb.Helpers;

namespace MVCWeb.Controllers
{
    public class DocController : BaseController
    {
        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult Index()
        {

            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            int communityId = Convert.ToInt32(Session["CommunityId"]);
            Community community = dc.Communities.Single<Community>(c => c.CommunityId == communityId);

            ViewData["CommunityName"] = community.Name;
            ViewData["CommunityId"] = community.CommunityId;
            ViewData["CurrentView"] = string.Format("Welcome to {0}", community.Name);

            if (string.IsNullOrEmpty(community.ImgUrl) == false)
            {
                ViewData["CommunityImgUrl"] = string.Format("<img alt='' class='CommunityImage'  src='{0}' />", Url.Content("~/" + community.ImgUrl));
            }
            else
            {
                ViewData["CommunityImgUrl"] = string.Empty;
            }

            return View();



            //if (Session["CommunityId"] == null)
            //{
            //    return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            //}

            //int communityId = Convert.ToInt32(Session["CommunityId"]);
            //Community community = dc.Communities.Single<Community>(c => c.CommunityId == communityId);

            //ViewData["CommunityName"] = community.Name;
            //ViewData["CommunityId"] = community.CommunityId;
            //ViewData["CurrentView"] = string.Format("Welcome to {0}", community.Name);

            //if (string.IsNullOrEmpty(community.ImgUrl) == false)
            //{
            //    ViewData["CommunityImgUrl"] = string.Format("<img alt='' class='CommunityImage'  src='{0}' />", Url.Content("~/" + community.ImgUrl));
            //}
            //else
            //{
            //    ViewData["CommunityImgUrl"] = string.Empty;
            //}

            //IList<DocCategory> topCategories = this.dc.DocCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false) && (c.DocCategoryId > 0)).OrderBy(c => c.Category).ToList();
            //return View(topCategories);
        }

        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult IndexCommunityDoc()
        {

            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            int communityId = Convert.ToInt32(Session["CommunityId"]);
            
            IList<CommunityDoc> communityDocs = dc.CommunityDocs.Where<CommunityDoc>(x => x.CommunityId == communityId).ToList<CommunityDoc>();
            IList<Doc> docs = new List<Doc>();
            foreach (CommunityDoc item in communityDocs)
            {
                docs.Add(item.Doc);
            }

            return View(docs);
        }

        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult IndexOfLegislative()
        {

            var Docs = from x in dc.Docs
                       where x.DocCategoryId == 1
                       group x by x.DocCategory into r
                       select new { Category = r.Key, Docs = r };

            IList<DocCategory> docCategories = new List<DocCategory>();
            foreach (var item in Docs)
            {
                DocCategory category = new DocCategory();
                category.Category = Convert.ToString(item.Category.Category);
                category.DocCategoryId = item.Category.DocCategoryId;
                category.Docs = item.Category.Docs;
                docCategories.Add(category);
            }

            return View(docCategories.ToList<DocCategory>());
        }

        [Authorize]
        public ActionResult Details(int id)
        {
            Doc doc = dc.Docs.First<Doc>(x => x.DocId == id);
            return View(doc);
        }

        [Authorize]
        public ActionResult Download(string path)
        {
            return RedirectToAction("Index");
        }

        //
        // GET: /Doc/Create
        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult Create()
        {

            if (Session["CommunityId"] == null)
            {
                return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
            }

            int communityId = Convert.ToInt32(Session["CommunityId"]);
            ViewData["CommunityId"] = communityId;

            DocumentNewViewData data = new DocumentNewViewData();
            IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList();
            IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

            data.Categories = topCategories;

            var subCategories = new CascadingSelectList(allCategories, "ParentDocCategoryId", "DocCategoryId", "Category");
            ViewData["DocCategoryId"] = subCategories;

            data.Communities = dc.Communities.OrderBy(c => c.Name).ToList<Community>();
            data.Document = new Doc();
            return View(data);
        }


        //
        // GET: /Doc/CreateLegislativeDoc
        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult CreateLegislativeDoc()
        {
            DocumentNewViewData data = new DocumentNewViewData();
            data.Document = new Doc();
            data.Categories = this.dc.DocCategories.OrderBy(c => c.Category).ToList<DocCategory>();
            return View(data);
        }

        //
        // POST: /Doc/CreateLegislativeDoc
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult CreateLegislativeDoc(Doc documentToCreate, FormCollection collection)
        {
            try
            {
                Server.ScriptTimeout = 300;
                int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
                int catId = 1;
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    if (hpf.ContentLength == 0)
                        continue;

                    //uploads\{companyid}\Category{Categoryid}\doc
                    string savePath = @"Uploads\" + companyId + "\\" + catId + "\\Doc\\";

                    string physicPath = AppDomain.CurrentDomain.BaseDirectory + savePath;
                    if (!Directory.Exists(physicPath))
                    {
                        Directory.CreateDirectory(physicPath);
                    }

                    string sFilename = Path.GetFileName(hpf.FileName);
                    string savedFileName = Path.Combine(physicPath, sFilename);

                    string extension = Path.GetExtension(hpf.FileName).ToLower();

                    int file_append = 0;
                    while (System.IO.File.Exists(Path.Combine(physicPath, sFilename)))
                    {
                        file_append++;
                        sFilename = System.IO.Path.GetFileNameWithoutExtension(hpf.FileName) + file_append.ToString() + extension;
                    }
                    // Replace Filename to safe filename.
                    sFilename = FileHelper.ConvertToSafeFilename(sFilename);
                    savedFileName = Path.Combine(physicPath, sFilename);

                    hpf.SaveAs(savedFileName);

                    documentToCreate.Size = hpf.ContentLength;
                    documentToCreate.FullPath = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);
                    documentToCreate.URLLink = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);

                }

                documentToCreate.DocCategoryId = 1; // Fixed category for Legislative Updates;
                documentToCreate.ModifiedDate = DateTime.Now;
                this.dc.Docs.InsertOnSubmit(documentToCreate);
                this.dc.SubmitChanges();
                
                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("IndexOfLegislative");
                }
                else
                {
                    return Redirect(Request.RawUrl);
                }
            }
            catch
            {
                FileInfo oldFile = new FileInfo(documentToCreate.FullPath);
                if (oldFile.Exists)
                {
                    oldFile.Delete();
                }
                return RedirectToAction("IndexOfLegislative");
            }

        }

        //
        // GET: /Doc/Edit/5
        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult EditLegislativeDoc(int? id)
        {
            DocumentNewViewData data = new DocumentNewViewData();
            data.Document = dc.Docs.First<Doc>(x => x.DocId == id);
            data.Categories = this.dc.DocCategories.OrderBy(c => c.Category).ToList<DocCategory>();
            return View(data);
        }

        //
        // POST: /Doc/Edit/5

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult EditLegislativeDoc(int id, FormCollection collection)
        {
            try
            {
                Server.ScriptTimeout = 300;
                int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
                int catId = 1;

                Doc doc = dc.Docs.First<Doc>(x => x.DocId == id);

                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    if (hpf.ContentLength == 0)
                        continue;

                    FileInfo oldFile = new FileInfo(doc.FullPath);
                    if (oldFile.Exists)
                    {
                        oldFile.Delete();
                    }                    

                    string savePath = @"Uploads\" + companyId + "\\" + catId + "\\Doc\\";

                    string physicPath = AppDomain.CurrentDomain.BaseDirectory + savePath;
                    if (!Directory.Exists(physicPath))
                    {
                        Directory.CreateDirectory(physicPath);
                    }

                    string sFilename = Path.GetFileName(hpf.FileName);
                    string savedFileName = Path.Combine(physicPath, sFilename);

                    string extension = Path.GetExtension(hpf.FileName).ToLower();

                    int file_append = 0;
                    while (System.IO.File.Exists(Path.Combine(physicPath, sFilename)))
                    {
                        file_append++;
                        sFilename = System.IO.Path.GetFileNameWithoutExtension(hpf.FileName)
                                         + file_append.ToString()
                                         + extension;
                    }

                    // Replace Filename to safe filename.
                    sFilename = FileHelper.ConvertToSafeFilename(sFilename);
                    savedFileName = Path.Combine(physicPath, sFilename);

                    hpf.SaveAs(savedFileName);

                    doc.Size = hpf.ContentLength;
                    doc.FullPath = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);
                    doc.URLLink = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);

                }
                doc.Desc = collection["Desc"];
                doc.ModifiedDate = DateTime.Now;
                doc.DocCategoryId = catId;
                UpdateModel<Doc>(doc);
                dc.SubmitChanges();
                
                if (collection["IsRedirectToView"].Equals("true"))
                {
                    return RedirectToAction("IndexOfLegislative");
                }
                else
                {                    
                    return Redirect(Request.RawUrl);
                }
                
            }
            catch
            {
                return RedirectToAction("IndexOfLegislative");
            }
        }

        //
        // POST: /Doc/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult Create(Doc doc, FormCollection collection)
        {
            try
            {
                Server.ScriptTimeout = 300;
                int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
                int communityId = Convert.ToInt32(Session["CommunityId"]);
                int categoryId = doc.DocCategoryId.HasValue ? Convert.ToInt32(doc.DocCategoryId) : Convert.ToInt32(collection["DocParentCategoryId"]);
                if (ModelState.IsValid)
                {

                    foreach (string file in Request.Files)
                    {
                        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                        if (hpf.ContentLength == 0)
                            continue;


                        //uploads\{companyid}\{communityid}\{categoryid}\doc
                        string savePath = @"Uploads\" + companyId + "\\" + communityId + "\\" + categoryId + "\\Doc\\";

                        string physicPath = AppDomain.CurrentDomain.BaseDirectory + savePath;
                        if (!Directory.Exists(physicPath))
                        {
                            Directory.CreateDirectory(physicPath);
                        }

                        string sFilename = Path.GetFileName(hpf.FileName);
                        string savedFileName = Path.Combine(physicPath, sFilename);

                        string extension = Path.GetExtension(hpf.FileName).ToLower();

                        int file_append = 0;
                        while (System.IO.File.Exists(Path.Combine(physicPath, sFilename)))
                        {
                            file_append++;
                            sFilename = System.IO.Path.GetFileNameWithoutExtension(hpf.FileName)
                                             + file_append.ToString()
                                             + extension;
                        }

                        // Replace Filename to safe filename.
                        sFilename = FileHelper.ConvertToSafeFilename(sFilename);

                        savedFileName = Path.Combine(physicPath, sFilename);

                        hpf.SaveAs(savedFileName);
                        
                        doc.Size = hpf.ContentLength;
                        doc.FullPath = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);
                        doc.URLLink = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);

                    }
                    doc.DocCategoryId = categoryId;
                    doc.ModifiedDate = DateTime.Now;
                    dc.Docs.InsertOnSubmit(doc);
                    dc.SubmitChanges();

                    CommunityDoc communityDoc = new CommunityDoc();
                    communityDoc.DocId = doc.DocId;

                    communityDoc.CommunityId = communityId;
                    communityDoc.MeetingId = 1;
                    dc.CommunityDocs.InsertOnSubmit(communityDoc);
                    dc.SubmitChanges();

                    if (collection["IsSaveNew"].Equals("true"))
                    {
                        return RedirectToAction("Create");
                    }
                    else
                    {
                        return RedirectToAction("Index");
                    }
                }
                else
                {
                    DocumentNewViewData data = new DocumentNewViewData();
                    IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList();
                    IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

                    data.Categories = topCategories;

                    var subCategories = new CascadingSelectList(allCategories, "ParentDocCategoryId", "DocCategoryId", "Category");
                    ViewData["DocCategoryId"] = subCategories;

                    data.Document = new Doc();
                    return View(data);
                }
            }
            catch
            {
                FileInfo oldFile = new FileInfo(doc.FullPath);
                if (oldFile.Exists)
                {
                    oldFile.Delete();
                }
                return RedirectToAction("Create");
            }
        }

        //
        // GET: /Doc/Edit/5
        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult Edit(int? id)
        {
            Doc doc = dc.Docs.First<Doc>(x => x.DocId == id);
            var docCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).ToList<DocCategory>();
            
            IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList();
            IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

            var categories = new SelectList(topCategories, "DocCategoryId", "Category", doc.DocCategory.ParentDocCategoryId == 0 ? doc.DocCategory.DocCategoryId : doc.DocCategory.ParentDocCategoryId);
            ViewData["ParentCategoryId"] = categories;            
            
            var subCategories = new CascadingSelectList(allCategories, "ParentDocCategoryId", "DocCategoryId", "Category");
            ViewData["DocCategoryId"] = subCategories;

            CommunityDoc communityDoc = dc.CommunityDocs.Single<CommunityDoc>(x => x.DocId == doc.DocId);

            //var communities = dc.Communities.OrderBy(c => c.Name).ToList<Community>();
            //ViewData["CommunityId"] = new SelectList(communities, "CommunityId", "Name", communityDoc.CommunityId);
            ViewData["CommunityId"] = communityDoc.CommunityId;
            return View(doc);
        }

        //
        // POST: /Doc/Edit/5

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                Server.ScriptTimeout = 300;
                int companyId = Convert.ToInt32(ConfigurationManager.AppSettings["CompanyId"]);
                int communityId = Convert.ToInt32(collection["CommunityId"]);
                int categoryId = Convert.ToInt32(collection["DocCategoryId"]);
                if (categoryId == 0) categoryId = Convert.ToInt32(collection["ParentCategoryId"]);
                //int categoryId = doc.DocCategoryId.HasValue ? Convert.ToInt32(doc.DocCategoryId) : Convert.ToInt32(collection["DocParentCategoryId"]);
                //DocCategory docCat = this.dc.DocCategories.Single(c => c.DocCategoryId == Int32.Parse(collection["DocCategoryId"]));
                //if (docCat.ParentDocCategoryId == 0)
                //{
                //    ModelState.AddModelError("", "Category must be selected as sub category");
                //}

                if (ModelState.IsValid)
                {
                    Doc doc = dc.Docs.First<Doc>(x => x.DocId == id);

                    foreach (string file in Request.Files)
                    {
                        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                        if (hpf.ContentLength == 0)
                            continue;

                        FileInfo oldFile = new FileInfo(doc.FullPath);
                        if (oldFile.Exists)
                        {
                            oldFile.Delete();
                        }                       

                        string savePath = @"Uploads\" + companyId + "\\" + communityId + "\\" + categoryId + "\\Doc\\";

                        string physicPath = AppDomain.CurrentDomain.BaseDirectory + savePath;
                        if (!Directory.Exists(physicPath))
                        {
                            Directory.CreateDirectory(physicPath);
                        }

                        string sFilename = Path.GetFileName(hpf.FileName);
                        string savedFileName = Path.Combine(physicPath, sFilename);

                        string extension = Path.GetExtension(hpf.FileName).ToLower();

                        int file_append = 0;
                        while (System.IO.File.Exists(Path.Combine(physicPath, sFilename)))
                        {
                            file_append++;
                            sFilename = System.IO.Path.GetFileNameWithoutExtension(hpf.FileName)
                                             + file_append.ToString()
                                             + extension;
                        }

                        // Replace Filename to safe filename.
                        sFilename = FileHelper.ConvertToSafeFilename(sFilename);
                        savedFileName = Path.Combine(physicPath, sFilename);

                        hpf.SaveAs(savedFileName);

                        doc.Size = hpf.ContentLength;
                        doc.FullPath = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);
                        doc.URLLink = Url.Content("~/" + savePath.Replace('\\', '/') + sFilename);

                    }
                    doc.Desc = collection["Desc"];
                    doc.DocCategoryId = categoryId;
                    UpdateModel<Doc>(doc);
                    dc.SubmitChanges();

                    //CommunityDoc communityDoc = dc.CommunityDocs.Single<CommunityDoc>(d => d.DocId == doc.DocId);
                    //communityDoc.CommunityId = communityId;
                    //communityDoc.MeetingId = 1;
                   // dc.SubmitChanges();

                    if (collection["IsRedirectToView"].Equals("true"))
                    {                        
                        return RedirectToAction("Index");                       
                    }
                    else
                    {
                        //return RedirectToAction("Edit", new { @id = id}); 
                        return Redirect(Request.RawUrl);
                    }                    
                }
                else
                {
                    Doc doc = dc.Docs.First<Doc>(x => x.DocId == id);
                    var docCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).ToList<DocCategory>();


                    IList<DocCategory> finalCategories = new List<DocCategory>();

                    IList<DocCategory> allCategories = dc.DocCategories.Where(c => c.DocCategoryId != 1).OrderBy(c => c.Category).ToList<DocCategory>();
                    IList<DocCategory> topCategories = allCategories.Where(c => (c.ParentDocCategoryId == 0 || c.ParentDocCategoryId.HasValue == false)).OrderBy(c => c.Category).ToList<DocCategory>();

                    foreach (var item in topCategories)
                    {
                        finalCategories.Add(item);
                        IList<DocCategory> subCategories = allCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId).OrderBy(c => c.Category).ToList<DocCategory>();
                        foreach (var subitem in subCategories)
                        {
                            subitem.Category = "-- " + subitem.Category;
                            finalCategories.Add(subitem);
                        }
                    }
                    ViewData["DocCategoryId"] = new SelectList(finalCategories, "DocCategoryID", "Category", doc.DocCategoryId);

                    CommunityDoc communityDoc = dc.CommunityDocs.Single<CommunityDoc>(x => x.DocId == doc.DocId);

                    var communities = dc.Communities.OrderBy(c => c.Name).ToList<Community>();
                    ViewData["CommunityId"] = new SelectList(communities, "CommunityId", "Name", communityDoc.CommunityId);
                    return View(doc);
                }
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        // Only Administrators can delete document.
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            try
            {
                Doc doc = dc.Docs.First<Doc>(x => x.DocId == id);

                FileInfo oldFile = new FileInfo(doc.FullPath);
                if (oldFile.Exists)
                {
                    oldFile.Delete();
                }
                dc.Docs.DeleteOnSubmit(doc);
                dc.SubmitChanges();
                return RedirectToAction("List", "Community");
            }
            catch
            {
                return RedirectToAction("List", "Community");
            }
        }

        [CompressFilter]
        [Authorize(Roles="Administrators, PropertyManagers")]
        public ActionResult List(int categoryId)
        {
            try
            {
                if (Session["CommunityId"] == null)
                {
                    return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
                }

                int communityId = Convert.ToInt32(Session["CommunityId"]);

                Community community = dc.Communities.Single(c => c.CommunityId == communityId);
                ViewData["CommunityName"] = community.Name;
                ViewData["CommunityId"] = community.CommunityId;
                ViewData["CurrentView"] = string.Format("Welcome to {0}", community.Name);
                if (string.IsNullOrEmpty(community.ImgUrl) == false)
                {
                    ViewData["CommunityImgUrl"] = string.Format("<img alt='' class='CommunityImage'  src='{0}' />", Url.Content("~/" + community.ImgUrl));
                }
                else
                {
                    ViewData["CommunityImgUrl"] = string.Empty;
                }

                IList<CommunityDoc> communityDoc = dc.CommunityDocs.Where(x => x.CommunityId == communityId).ToList();

                IList<Doc> listDocs = new List<Doc>();
                foreach (var item in communityDoc)
                {
                    listDocs.Add(item.Doc);
                }
                var Docs = from x in dc.Docs
                           where (from c in dc.CommunityDocs
                                  where c.CommunityId == communityId
                                  select c.DocId).Contains(x.DocId)
                           group x by x.DocCategory into r
                           select new { Category = r.Key, Docs = r };

                IList<DocCategory> docCategories = new List<DocCategory>();
                foreach (var item in Docs)
                {
                    try
                    {
                        DocCategory category = new DocCategory();
                        category.Category = item.Category.Category;
                        category.DocCategoryId = item.Category.DocCategoryId;
                        category.ParentDocCategoryId = item.Category.ParentDocCategoryId;

                        foreach (var doc in item.Docs)
                        {
                            category.Docs.Add(doc);
                        }
                        docCategories.Add(category);
                    }
                    catch { }
                }
                // IList<DocCategory> list = docCategories.Where(c => c.ParentDocCategoryId == categoryId).ToList(); : original
                IList<DocCategory> list = docCategories.Where(c => c.DocCategoryId == categoryId || c.ParentDocCategoryId == categoryId).ToList(); // :  modified
                return View(list);

                //return View(docCategories.Where(c => c.ParentDocCategoryId == categoryId));
            }
            catch
            {
            }
            return View(new List<DocCategory>());
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult ChangeStatus(int id)
        {
            Doc doc = this.dc.Docs.First(x => x.DocId == id);
            doc.Active = !doc.Active;
            this.dc.SubmitChanges();
            return View();
        }

    }
}
